package cn.wzut.edut.dao.inf;

import cn.wzut.edut.dao.SnowflakeIdWorker;
import cn.wzut.edut.dao.mapper.SysMenuMapper;
import cn.wzut.edut.dao.mapper.SysRoleMapper;
import cn.wzut.edut.dao.mapper.SysRoleMenuRelMapper;
import cn.wzut.edut.dao.pojo.table.*;
import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.LinkedList;
import java.util.List;

/**
 * 菜单功能管理
 */
@Repository
public class Menu {
    @Autowired
    private SysRoleMenuRelMapper sysRoleMenuRelMapper;
    @Autowired
    private SysMenuMapper sysMenuMapper;
    /**
     * 查询菜单
     * 输入菜单id
     * @return 菜单
     */
    public SysMenu selectSysMenu(String id){
        /**
         * q1：以菜单id查找菜单
         */
        QueryWrapper<SysMenu> q1 = new QueryWrapper<>();
        q1.in("id",id);
        return sysMenuMapper.selectOne(q1);
    }
    /**
     * 查询角色权限
     * 输入角色id的列表
     * @return 菜单列表
     */
    public List<SysMenu> selectSysMenus(List<String> role_id){
        /**
         * 构造条件q1，q2
         * q1：以角色id查找菜单id
         * q2：以菜单id查找
         */
        QueryWrapper<SysRoleMenuRel> q1 = new QueryWrapper<>();
        q1.in("role_id",role_id);
        List<SysRoleMenuRel> sysRoleMenuRels = sysRoleMenuRelMapper.selectList(q1);
        List<String> roles = new LinkedList<>();
        for(int i = 0;i<sysRoleMenuRels.size();i++){
            roles.add(sysRoleMenuRels.get(i).getMenuId());
        }
        QueryWrapper<SysMenu> q2 = new QueryWrapper<>();
        q2.select("DISTINCT id,menu_name,icon_url,parent_menu_id,link_url").in("id",roles);
        return sysMenuMapper.selectList(q2);
    }
    /**
     * 按照菜单id删除用户菜单表
     */
    public boolean delSysRoleMenuRel(String id){
        /**
         * queryWrapper：按照菜单id删除角色菜单关系表
         */
        try {
            QueryWrapper<SysRoleMenuRel> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("menu_id", id);
            sysRoleMenuRelMapper.delete(queryWrapper);
        }catch (Exception e){
            return false;
        }
        return true;
    }
    /**
     * 按照菜单id删除菜单表
     */
    public boolean delSysMenu(String id){
        /**
         * queryWrapper：按照菜单id删除菜单表
         */
        try {
            delSysRoleMenuRel(id);
            QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("id", id);
            sysMenuMapper.delete(queryWrapper);
        }catch (Exception e){
            return false;
        }
        return true;
    }

    /**
     * 添加菜单
     * 需要随便向pojo中输入一个id
     */
    public String insertSysMenu(SysMenu sysMenu){
        try {
//            SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker();
//            sysMenu.setId(String.valueOf(snowflakeIdWorker.nextId()));
            sysMenuMapper.insert(sysMenu);
        }catch (Exception e){
            return sysMenu.getId();
        }
        return sysMenu.getId();
    }
    /**
     * 更新菜单
     */
    public boolean updateSysMenu(SysMenu sysMenu){
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(sysMenu.getId());
        sysMenuMapper.update(sysMenu,queryWrapper);
        return true;
    }
    /**
     * 查询菜单数
     */
    public long countSysMenu(){
        return sysMenuMapper.selectCount(null);
    }
    /**
     * 查询所有菜单
     */
    public List<SysMenu> selectAllSysUser(){
        return sysMenuMapper.selectList(null);
    }
    /**
     * 分页
     * 查询所有菜单
     * 传入需要的页码current和每页显示的数量size
     */
    public List<SysMenu> selectAllSysUserPage(long current,long size){
        return sysMenuMapper.selectPage(new Page<SysMenu>(current,size),null).getRecords();
    }
    /**
     * 分页
     * 查询菜单
     * 输入菜单名
     * @return 菜单
     */
    public List<SysMenu> selectSysMenuInNamePage(long current,long size,String name){
        /**
         * q1：以菜单名查找菜单
         */
        QueryWrapper<SysMenu> q1 = new QueryWrapper<>();
        q1.in("menu_name",name);
        return sysMenuMapper.selectPage(new Page<SysMenu>(current,size),q1).getRecords();
    }
    /**
     * 查询角色权限
     * 输入角色id的列表
     * @return 菜单列表
     */
    public List<SysMenu> selectSysMenuInRole(String role_id){
        /**
         * 构造条件q1，q2
         * q1：以角色id查找菜单id
         * q2：以菜单id查找
         */
        QueryWrapper<SysRoleMenuRel> q1 = new QueryWrapper<>();
        q1.in("role_id",role_id);
        List<SysRoleMenuRel> sysRoleMenuRels = sysRoleMenuRelMapper.selectList(q1);
        List<SysMenu> list = new LinkedList<>();
        for(int i = 0;i<sysRoleMenuRels.size();i++){
            QueryWrapper<SysMenu> q2 = new QueryWrapper<>();
            q2.in("id",sysRoleMenuRels.get(i).getMenuId());
            list.add(sysMenuMapper.selectOne(q2));
        }
        return list;
    }
    /**173
     * 查询菜单总数
     * 输入菜单名
     * @return 菜单
     */
    public long countSysMenuInNamePage(String name){
        /**
         * q1：以菜单名查找菜单
         */
        QueryWrapper<SysMenu> q1 = new QueryWrapper<>();
        q1.in("menu_name",name);
        return sysMenuMapper.selectCount(q1);
    }
    @Autowired
    private SysRoleMapper sysRoleMapper;
    /**
     * 分页
     * 查询角色权限
     * 输入菜单id的列表
     * @return 角色列表
     */
    public List<SysRole> selectRoleInMenuPage(String menu_id,long current,long size){
        /**
         * 构造条件q1，q2
         * q1：以角色id查找菜单id
         * q2：以菜单id查找
         */
        QueryWrapper<SysRoleMenuRel> q1 = new QueryWrapper<>();
        q1.in("menu_id",menu_id);
        List<SysRoleMenuRel> sysRoleMenuRels = sysRoleMenuRelMapper.selectList(q1);
        List<SysRole> list1 = new LinkedList<>();
        for(int i = 0;i<sysRoleMenuRels.size();i++){
            QueryWrapper<SysRole> q2 = new QueryWrapper<>();
            q2.in("id",sysRoleMenuRels.get(i).getRoleId());
            list1.add(sysRoleMapper.selectOne(q2));
        }
        List<SysRole> list2 = new LinkedList<>();
        for(long i = current*size-size;i<current*size;i++){list2.add(list1.get((int) i));}
        return list2;
    }
    /**
     * 查询角色权限
     * 输入菜单id的列表
     * @return 角色列表
     */
    public long countRoleInMenu(String menu_id){
        /**
         * 构造条件q1，q2
         * q1：以角色id查找菜单id
         * q2：以菜单id查找
         */
        QueryWrapper<SysRoleMenuRel> q1 = new QueryWrapper<>();
        q1.in("menu_id",menu_id);
        return sysRoleMenuRelMapper.selectCount(q1);
    }

    /**
     * ======================================================================================================================================================================
     * ======================================================================================================================================================================
     * 已整理
     * ======================================================================================================================================================================
     * ======================================================================================================================================================================
     */

    /**
     * ===================================================================================================
     * 基础查询
     * ===================================================================================================
     */

    /**
     * 查询菜单信息
     */
    public List<SysMenu> selectSysMenu(String id,String menu_name,String icon_url,String parent_menu_id,String link_url){
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(!StringUtils.isEmpty(id),"id",id);
        queryWrapper.in(!StringUtils.isEmpty(menu_name),"menu_name",menu_name);
        queryWrapper.in(!StringUtils.isEmpty(icon_url),"icon_url",icon_url);
        queryWrapper.in(!StringUtils.isEmpty(parent_menu_id),"parent_menu_id",parent_menu_id);
        queryWrapper.in(!StringUtils.isEmpty(link_url),"link_url",link_url);
        return sysMenuMapper.selectList(queryWrapper);
    }
    /**
     * 查询菜单信息数量
     */
    public long countSysMenu(String id,String menu_name,String icon_url,String parent_menu_id,String link_url){
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(!StringUtils.isEmpty(id),"id",id);
        queryWrapper.in(!StringUtils.isEmpty(menu_name),"menu_name",menu_name);
        queryWrapper.in(!StringUtils.isEmpty(icon_url),"icon_url",icon_url);
        queryWrapper.in(!StringUtils.isEmpty(parent_menu_id),"parent_menu_id",parent_menu_id);
        queryWrapper.in(!StringUtils.isEmpty(link_url),"link_url",link_url);
        return sysMenuMapper.selectCount(queryWrapper);
    }
    /**
     * 查询菜单信息
     * 分页
     */
    public List<SysMenu> selectSysMenuPage(String id,String menu_name,String icon_url,String parent_menu_id,String link_url,long current,long size){
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(!StringUtils.isEmpty(id),"id",id);
        queryWrapper.in(!StringUtils.isEmpty(menu_name),"menu_name",menu_name);
        queryWrapper.in(!StringUtils.isEmpty(icon_url),"icon_url",icon_url);
        queryWrapper.in(!StringUtils.isEmpty(parent_menu_id),"parent_menu_id",parent_menu_id);
        queryWrapper.in(!StringUtils.isEmpty(link_url),"link_url",link_url);
        return sysMenuMapper.selectPage(new Page<SysMenu>(current,size),queryWrapper).getRecords();
    }
    /**
     * 查询菜单信息
     * 列表查询
     */
    public List<SysMenu> selectSysMenuList(List<String> id,List<String> menu_name,List<String> icon_url,List<String> parent_menu_id,List<String> link_url){
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(id!=null,"id",id);
        queryWrapper.in(id!=null,"menu_name",menu_name);
        queryWrapper.in(id!=null,"icon_url",icon_url);
        queryWrapper.in(id!=null,"parent_menu_id",parent_menu_id);
        queryWrapper.in(id!=null,"link_url",link_url);
        return sysMenuMapper.selectList(queryWrapper);
    }
    /**
     * 查询菜单信息数量
     * 列表查询
     */
    public long countSysMenuList(List<String> id,List<String> menu_name,List<String> icon_url,List<String> parent_menu_id,List<String> link_url){
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(id!=null,"id",id);
        queryWrapper.in(id!=null,"menu_name",menu_name);
        queryWrapper.in(id!=null,"icon_url",icon_url);
        queryWrapper.in(id!=null,"parent_menu_id",parent_menu_id);
        queryWrapper.in(id!=null,"link_url",link_url);
        return sysMenuMapper.selectCount(queryWrapper);
    }
    /**
     * 查询菜单信息
     * 列表查询
     * 分页
     */
    public List<SysMenu> selectSysMenuListPage(List<String> id,List<String> menu_name,List<String> icon_url,List<String> parent_menu_id,List<String> link_url,long current,long size){
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(id!=null,"id",id);
        queryWrapper.in(id!=null,"menu_name",menu_name);
        queryWrapper.in(id!=null,"icon_url",icon_url);
        queryWrapper.in(id!=null,"parent_menu_id",parent_menu_id);
        queryWrapper.in(id!=null,"link_url",link_url);
        return sysMenuMapper.selectPage(new Page<SysMenu>(current,size),queryWrapper).getRecords();
    }

    /**
     * ===================================================================================================
     * 基础删除
     * ===================================================================================================
     */

    /**
     * ===================================================================================================
     * 基础更新
     * ===================================================================================================
     */

    /**
     * ===================================================================================================
     * 基础新增
     * ===================================================================================================
     */

    /**
     * ===================================================================================================
     * 级联删除
     * ===================================================================================================
     */

    /**
     * ===================================================================================================
     * 复杂查询
     * ===================================================================================================
     */


}
